#include "invocation.h"
#include "gerror.h"
#include "log.h"
#include <Python.h>

using namespace std;

#define PY_INV_MODULE 	"invocation"
#define PY_INV_FUNC 	"parseArgs"

Invocation::Invocation(int argc, char **argv)
{
	PyObject *pModule, *pFunc, *pList, *pArgs;
	PyObject *pReturn, *pValue, *pItem;
	string path;
	int value;

	pModule = PyImport_ImportModule(PY_INV_MODULE);
	pythonError(pModule, PYERR_MODULE(PY_INV_MODULE));

	pFunc = PyObject_GetAttrString(pModule, PY_INV_FUNC);
	pythonError(pFunc, PYERR_ATTR(PY_INV_FUNC));
	Py_DECREF(pModule);

	pList = PyList_New(0);
	for (int i = 0; i < argc; i++)
		PyList_Append(pList, PyString_FromString(argv[i]));
	pArgs = PyTuple_Pack(1, pList);

	pReturn = PyObject_CallObject(pFunc, pArgs);
	pythonError(pReturn, PYERR_FUNC(PY_INV_FUNC));
	Py_DECREF(pArgs);
	Py_DECREF(pList);

	pValue = PyObject_GetAttrString(pReturn, "decode");
	pythonError(pValue, PYERR_ATTR("decode"));
	decode = (bool) PyInt_AsLong(pValue);
	Py_DECREF(pValue);

	pValue = PyObject_GetAttrString(pReturn, "wordSize");
	pythonError(pValue, PYERR_ATTR("wordSize"));
	for (Py_ssize_t i = 0; i < PyList_Size(pValue); i++)
	{
		pItem = PyList_GetItem(pValue, i);
		pythonError(pItem, PYERR_ATTR("list item"));
		value = PyInt_AsLong(pItem);
		wordSize.push_back(value);
		Py_DECREF(pItem);
	}Py_DECREF(pValue);

	pValue = PyObject_GetAttrString(pReturn, "M");
	pythonError(pValue, PYERR_ATTR("M"));
	for (Py_ssize_t i = 0; i < PyList_Size(pValue); i++)
	{
		pItem = PyList_GetItem(pValue, i);
		pythonError(pItem, PYERR_ATTR("list item"));
		value = PyInt_AsLong(pItem);
		M.push_back(value);
		Py_DECREF(pItem);
	}Py_DECREF(pValue);

	pValue = PyObject_GetAttrString(pReturn, "verbose");
	pythonError(pValue, PYERR_ATTR("verbose"));
	verbose = (bool) PyInt_AsLong(pValue);
	Py_DECREF(pValue);

	pValue = PyObject_GetAttrString(pReturn, "debug");
	pythonError(pValue, PYERR_ATTR("debug"));
	debug = (bool) PyInt_AsLong(pValue);
	Py_DECREF(pValue);

	pValue = PyObject_GetAttrString(pReturn, "benchmark");
	pythonError(pValue, PYERR_ATTR("benchmark"));
	benchmark = (bool) PyInt_AsLong(pValue);
	Py_DECREF(pValue);

	pValue = PyObject_GetAttrString(pReturn, "asserting");
	pythonError(pValue, PYERR_ATTR("asserting"));
	asserting = (bool) PyInt_AsLong(pValue);
	Py_DECREF(pValue);

	pValue = PyObject_GetAttrString(pReturn, "inPaths");
	pythonError(pValue, PYERR_ATTR("inPaths"));
	for (Py_ssize_t i = 0; i < PyList_Size(pValue); i++)
	{
		pItem = PyList_GetItem(pValue, i);
		pythonError(pItem, PYERR_ATTR("list item"));
		path = PyString_AsString(pItem);
		inPaths.push_back(string(path));
		Py_DECREF(pItem);
	}Py_DECREF(pValue);

	pValue = PyObject_GetAttrString(pReturn, "outPaths");
	pythonError(pValue, PYERR_ATTR("outPaths"));
	for (Py_ssize_t i = 0; i < PyList_Size(pValue); i++)
	{
		pItem = PyList_GetItem(pValue, i);
		pythonError(pItem, PYERR_ATTR("list item"));
		if (pItem == Py_None)
			path = string("");
		else
			path = PyString_AsString(pItem);
		outPaths.push_back(path);
		Py_DECREF(pItem);
	}
	Py_DECREF(pValue);
}
